package cn.edu.fzu.homemaking.company.controller;

import cn.edu.fzu.homemaking.common.SessionConstant;
import cn.edu.fzu.homemaking.company.api.OrderApi;
import cn.edu.fzu.homemaking.company.api.UserApi;
import cn.edu.fzu.homemaking.company.domain.entity.Demand;
import cn.edu.fzu.homemaking.company.domain.entity.CompanyUser;
import cn.edu.fzu.homemaking.company.domain.enums.DemandStatusEnum;
import cn.edu.fzu.homemaking.remoting.IpAddressApiImpl;
import cn.edu.fzu.homemaking.remoting.dto.IpAddressDto;
import cn.edu.fzu.homemaking.util.ResultUtil;
import cn.edu.fzu.homemaking.web.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author wanjie
 * @version 1.0
 */
@RestController
@RequestMapping("/api/company/auth/demand")
@Api(tags = "客户需求相关接口")
public class DemandController {

    @Resource
    private UserApi          userApi;

    @Resource
    private OrderApi         orderApi;

    @Resource
    private IpAddressApiImpl ipAddressApi;

    @GetMapping("/list")
    @ApiOperation(value = "客户需求列表查询", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "labelId", required = false, value = "标签id"),
            @ApiImplicitParam(name = "startTime", required = false, value = "开始时间"),
            @ApiImplicitParam(name = "endTime", required = false, value = "结束时间"),
            @ApiImplicitParam(name = "address", required = false, value = "地址"),
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result listDemand(Long labelId, String startTime, String endTime, String address, Integer pageNum,
                             Integer pageSize, HttpServletRequest request) {
        if (StringUtils.isBlank(address)) {
            String ip = request.getHeader("X-Real-IP");
            if (StringUtils.isNotBlank(ip)) {
                IpAddressDto addressDto = ipAddressApi.get(ip);
                if (addressDto != null) {
                    address = addressDto.getCity();
                }
            }
        }
        return userApi.list(labelId, startTime, endTime, address, DemandStatusEnum.WAIT, pageNum, pageSize);
    }


    @ApiOperation(value = "需求详情", httpMethod = "GET")
    @GetMapping("/info")
    public Result info(Long id) {
        return userApi.querySingleDemand(id);
    }


    @GetMapping("/order")
    @ApiOperation(value = "接需求单（下单）", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "需求单id", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result orderByDemand(Long id, HttpServletRequest request) {
        CompanyUser user = (CompanyUser) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        //获取相应需求单
        Demand demand = ResultUtil.parseValue(userApi.querySingleDemand(id), Demand.class);
        //生成相应订单
        orderApi.insertOrder(user.getCompany().getId(), demand.getUserId(), demand.getId(), demand.getLabelId(),
                demand.getDemand(), demand.getName(), demand.getAddress(), demand.getPhone(), demand.getEmail(),
                DateFormatUtils.format(demand.getTime(), "yyyy-MM-dd HH:mm:ss"), demand.getPrice(), 1);
        //修改需求单状态为：已接单
        userApi.updateDemandStatus(id, DemandStatusEnum.ACCEPT);
        return Result.SUCCESS;
    }

}
